package models

import (
	"errors"
	"fmt"
	"go-admin/global/orm"
	"strconv"
)

type VROrder struct {
	ZxOrder
	SignState    string `json:"signState,omitempty"`
	DesignState  string `json:"designState,omitempty"`
	BuildState   string `json:"buildState,omitempty"`
	RevisitState string `json:"revisitState,omitempty"`
	DesignPrice  int    `json:"designPrice,omitempty"` // 设计报价
	BuildCost    int    `json:"buildCost,omitempty" `  // 施工成本
}

// 获取VRSign
func (e *VROrder) Get() (VROrder, error) {
	var doc VROrder
	if e.Id == 0 {
		return doc, errors.New("OrderId is empty")
	}
	selectSql := "SELECT zo.*" +
		", zs.sign_state as sign_state" +
		", zd.design_state as design_state" +
		", zd.design_price as design_price" +
		", zb.build_state as build_state" +
		", zb.build_cost as build_cost" +
		", zr.revisit_state as revisit_state"
	where := " FROM zx_order zo" +
		" JOIN zx_sign zs on zs.id = zo.sign_id" +
		" JOIN zx_design zd on zd.id = zo.design_id" +
		" JOIN zx_build zb on zb.id = zo.build_id" +
		" JOIN zx_revisit zr on zr.id = zo.revisit_id" +
		" where zo.deleted_at IS NULL"
	where = fmt.Sprintf("%s AND zo.id = %d", where, e.Id)

	orm.Eloquent.Raw(selectSql + where).Scan(&doc)
	return doc, nil
}

// 获取ZxOrder带分页
func (e *VROrder) GetPage(pageSize int, pageIndex int) ([]VROrder, int, error) {
	var ret []VROrder

	selectSql := "SELECT zo.*" +
		", zs.sign_state as sign_state" +
		", zd.design_state as design_state" +
		", zd.design_price as design_price" +
		", zb.build_state as build_state" +
		", zb.build_cost as build_cost" +
		", zr.revisit_state as revisit_state"
	where := " FROM zx_order zo" +
		" JOIN zx_sign zs on zs.id = zo.sign_id" +
		" JOIN zx_design zd on zd.id = zo.design_id" +
		" JOIN zx_build zb on zb.id = zo.build_id" +
		" JOIN zx_revisit zr on zr.id = zo.revisit_id" +
		" where zo.deleted_at IS NULL"
	where = fmt.Sprintf("%s AND zo.create_by = %s", where, e.CreateBy)

	if e.CustomName != "" {
		where = fmt.Sprintf("%s AND zo.custom_tel like '%%%s%%'", where, e.CustomName)
	}
	if e.CustomTel != "" {
		where = fmt.Sprintf("%s AND zo.custom_tel = %s", where, e.CustomTel)
	}
	if e.CustomAddr != "" {
		where = fmt.Sprintf("%s AND zo.custom_addr like '%%%s%%'", where, e.CustomAddr)
	}
	if e.SignState != "" {
		where = fmt.Sprintf("%s AND zs.sign_state = '%s'", where, e.SignState)
	}
	if e.DesignState != "" {
		where = fmt.Sprintf("%s AND zd.design_state = '%s'", where, e.DesignState)
	}
	if e.BuildState != "" {
		where = fmt.Sprintf("%s AND zb.build_state = '%s'", where, e.BuildState)
	}
	if e.RevisitState != "" {
		where = fmt.Sprintf("%s AND zr.revisit_state = '%s'", where, e.RevisitState)
	}
	var count int
	selectCountSql := "SELECT count(*) " + where
	orm.Eloquent.Raw(selectCountSql).Count(&count)
	pageSql := " LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((pageIndex-1)*pageSize)
	selectSql = selectSql + where + pageSql + ";"
	orm.Eloquent.Raw(selectSql).Scan(&ret)
	return ret, count, nil
}

// 更新ZxOrder
func (e *VROrder) UpdateOther() (update VROrder, err error) {
	zxsign := ZxSign{Id: e.SignId, SignState: e.SignState}
	_, err = zxsign.Update()
	if err != nil {
		return
	}

	zxdesign := ZxDesign{Id: e.DesignId,
		DesignPrice: e.DesignPrice, DesignState: e.DesignState}
	_, err = zxdesign.Update()
	if err != nil {
		return
	}

	zxbuild := ZxBuild{Id: e.BuildId,
		BuildCost: e.BuildCost, BuildState: e.BuildState}
	_, err = zxbuild.Update()
	if err != nil {
		return
	}

	zxrevisit := ZxRevisit{Id: e.RevisitId, RevisitState: e.RevisitState}
	_, err = zxrevisit.Update()
	if err != nil {
		return
	}

	return
}
